/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bugsandcritters;
import java.util.ArrayList;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

/**
 *
 * @author Robert Gibbons
 */
import java.util.Random;

public class GridWorld {
    
    public static int CellsX = 25;
    public static int CellsY = 25;
    public static int CellsWidth = 32;
    public static int CellsHeight = 32;
    
    public static int Rocks = 0;
    public static int Flowers = 0;
    public static int Critters = 0;
    public static int Bugs = 0;
    
    public static BufferedImage GrassImage;
    
    public static ArrayList<GameObject> Children = new ArrayList<GameObject>();
    
    public GridWorld()
    {
        SpawnChildren();
    }
    
    public static GameObject CheckFree(double X,double Y,GameObject Checker)
    {
        if(X<0||X>CellsX*CellsWidth||Y<0||Y>CellsY*CellsHeight)
            return Checker;
        
        GameObject ToReturn = null;
        
        for(GameObject Object:Children)
            if(Object != Checker &&
                    Math.abs(X-Object.X)<CellsWidth/2.0  &&
                    Math.abs(Y-Object.Y)<CellsHeight/2.0)
                if(ToReturn == null || Object instanceof Rock)
                    ToReturn = Object;
        
        return ToReturn;

    }
    
    public static void SpawnChildren()
    {
        Children.clear();
        for(int i=0;i<Rocks;i++)
            AddChild(new Rock());
        for(int i=0;i<Bugs;i++)
            AddChild(new Bug());
        for(int i=0;i<Flowers;i++)
            AddChild(new Flower());
        for(int i=0;i<Critters;i++)
            AddChild(new Critter());
 
    }
    
    public static void AddChild(GameObject Object)
    {
        Children.add(Object);
        
        Random r = new Random();
        
        double X = r.nextInt(CellsX)*CellsWidth + CellsWidth/2;
        double Y = r.nextInt(CellsY)*CellsHeight + CellsHeight/2;
        
        while(CheckFree(X,Y,Object) != null)
        {
            X = r.nextInt(CellsX)*CellsWidth + CellsWidth/2;
            Y = r.nextInt(CellsY)*CellsHeight + CellsHeight/2; 
        }
        
        Object.SetPosition(X,Y);
        
        Object.Create();
    }
    
    public static void AddChild(GameObject Object, double X, double Y)
    {
        Children.add(Object);
        Object.SetPosition(X,Y);
        Object.Create();
    }
    
    public void DestroyObject(GameObject Object)
    {
        Children.remove(Object);
        Object.ShouldDestroy = true;
    }
    
    public void Update()
    {
        for(int i=0;i<Children.size();i++)
        {
            GameObject o = Children.get(i);
            //o.Update();
        }
    }
    
    public void Draw(Graphics g)
    {
        if(GrassImage == null)
        {
            try  {GrassImage = ImageIO.read(new File("Grass.png"));}
            catch(Exception e){}
        }
        else
            g.drawImage(GrassImage, 0,0, null);
        
        g.setColor(Color.BLACK);
        
        for(int x=0;x<CellsX;x++)
            g.drawLine(x*CellsWidth, 0, x*CellsWidth, CellsY*CellsHeight);

        for(int y=0;y<CellsY;y++)
            g.drawLine(0, y*CellsHeight, CellsX*CellsWidth,y*CellsHeight);
                
        for(GameObject Object:Children)
            Object.Draw(g);
    }
    
    
    
}
